﻿using System;
using System.Diagnostics;
using EasyDLL.Data;
using PostSharp.Laos;

namespace EasyDLL.AOP.Attributes
{
    /// <summary>
    /// 当方法发生异常时自动记录日志，也可以自己添加比较重要的日志信息。
    /// 日志的输出请到Log4Net.config中配置
    /// 该attribute依赖于postsharp编译
    /// </summary>
    [Serializable]
    public sealed class LoggingAttribute : OnMethodBoundaryAspect
    {
        public string ImportLogicalMsg;

        public LoggingAttribute(string importLogicalMsg)
        {
            ImportLogicalMsg = importLogicalMsg;
        }

        public LoggingAttribute()
        {

        }

        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {
            if (DataValidateWrapper.IsNotNullOrEmpty(ImportLogicalMsg))
            {
                string msg = string.Format("MethodName:{0} , {1}", eventArgs.Method.Name, ImportLogicalMsg);
                Logging.LoggingWrapper.Instance.LogInfo(msg);
            }

        }

        public override void OnExit(MethodExecutionEventArgs eventArgs)
        {
        }

        public override void OnException(MethodExecutionEventArgs eventArgs)
        {

        }
    }
}
